﻿#pragma execution_character_set("utf-8")
#include "otherinfo.h"
#include "ui_otherinfo.h"

OtherInfo::OtherInfo(const QString &email,const QString &username,QVariantMap &map,QWidget *parent) :
    BaseInfo(map["msgId"].toString(),map["createTime"].toLongLong(),parent),
    ui(new Ui::OtherInfo)
{
    ui->setupUi(this);
    //设置头像
    updateHeadImg(email);
    //设置名字
    ui->name_LB->setText(username);
    //设置内容
    QString type=map["type"].toString();
    if(type=="text"){//处理文字
        setText(map["content"].toString());
    }
    else if(type=="png")//处理图片
    {
        QString filePath=QString("./accountData/Res/%1/imageRes/%2").arg(email,this->msgId+"."+type);
        setImage(filePath);
    }
    else//处理文件
    {
        QString filePath=QString("./accountData/Res/%1/fileRes/%2").arg(email).arg(map["content"].toString()+"."+type);
        setFile(filePath);
    }
}

OtherInfo::~OtherInfo()
{
    delete ui;
}

void OtherInfo::updateHeadImg(const QString &email){
    if(QFile::exists(QString("./accountData/Res/%1/headImg/head.png").arg(email)))
    {
        ui->headImg_BT->setStyleSheet(QString("\
            QPushButton{\
                border:1px solid black;\
                border-radius:20px;\
                border-image: url('./accountData/Res/%1/headImg/head.png') 0 0 0 0 stretch stretch;\
            }\
        ").arg(email));
    }
}

void OtherInfo::setText(const QString &text){
    textEdit=new TextMessage(text,this);
    insertTextEdit();
    QFontMetrics fm(textEdit->font());
    textEdit->setMaximumWidth(fm.horizontalAdvance(text)+10);
}

void OtherInfo::setTemporatyImage(const QString &text,const QString &filePath){
    textEdit=new TextMessage(text,filePath,this);
    insertTextEdit();
    QFontMetrics fm(QFont("Microsoft YaHei UI",11));
    textEdit->setMaximumWidth(fm.horizontalAdvance(text)+10);
}

void OtherInfo::setImage(const QString &filePath){
    if (filePath.isEmpty()) {//判断文件路径是否为空串
        setText("[图片不存在]");
        return;
    }

    {
        QFileInfo fileInfo(filePath);
        if (!fileInfo.exists() || fileInfo.size() <= 0) {//判断文件是否存在
            setTemporatyImage("[图片]",filePath);//不存在
            return;
        }
    }
    QSize targetSize=scaleImage(filePath);
    if(targetSize.isEmpty()){//判断文件是否解析成功
        setText("[图片无法解析]");
        return ;
    }
    // 插入图片并设置其大小
    QTextImageFormat format;
    format.setName(filePath); // 设置图片的源路径
    format.setWidth(targetSize.width()); // 设置图片的宽度
    format.setHeight(targetSize.height()); // 设置图片的高度

    if(textEdit==nullptr){
        textEdit=new FileMessage(this);
        insertTextEdit();
        ((FileMessage*)textEdit)->setImage(format);
    }
}

void OtherInfo::setFile(const QString &filePath){
    if(fileInfo==nullptr){
        fileInfo=new FileInfo(filePath,this);
        connect(fileInfo,&FileInfo::downLoadFile,[this](){
            emit downLoadFile(this->msgId);
        });
        QHBoxLayout* content=(QHBoxLayout*)(ui->content_WT->layout());
        content->insertWidget(0,fileInfo);
        if(block==nullptr){
            block=new SpaceBlock(MessageStatus::SUCCESS,this);
            block->insertRight();
            content->addWidget(block);
        }
        QWidget* parentWidget=ui->content_WT->parentWidget();
        parentWidget->setFixedHeight(100+16);
        this->setFixedHeight(100+40+16);
    }
}

void OtherInfo::updateImage(QObject* sender,const QString &msgId){
    if(this->msgId==msgId){
        QString filePath=((TextMessage*)textEdit)->getFilePath();
        removeTextEdit();
        setImage(filePath);
        disconnect(sender,0,this,0);
    }
}
void OtherInfo::updateFile(QObject* sender,const QString &msgId){
    if(this->msgId==msgId){
        fileInfo->updateFileStatus();
        disconnect(sender,0,this,0);
    }
}

void OtherInfo::insertTextEdit(){
    if(textEdit!=nullptr){
        QHBoxLayout* content=(QHBoxLayout*)(ui->content_WT->layout());
        content->insertWidget(0,textEdit,6);
        if(block==nullptr){
            block=new SpaceBlock(MessageStatus::SUCCESS,this);
            content->addWidget(block,4);
        }
    }
}

void OtherInfo::insertTimeInfo(const QString &time){
    if(timeInfo==nullptr){
        timeInfo=new TimeInfo(time,this);
        ui->verticalLayout->insertWidget(0,timeInfo);
        if(textEdit!=nullptr){
            if(qobject_cast<FileMessage*>(textEdit)){
                this->setFixedHeight(height()+16+24);
            }
            else{
                TextMessage* baseMessage=qobject_cast<TextMessage*>(textEdit);
                if(baseMessage!=nullptr){
                    baseMessage->setIncreaseHight(24);
                }
            }
        }
        else
        {
            this->setFixedHeight(100+40+16+24);
        }
    }
}
